import requests
from config.all_paths import allure_report_prometheusData
from utils.file_handling.ini_handling import LoadIni
from utils.time_tool.time_control import Getdate


def push_message(webhook, content):
    requests.post(url=webhook, json=content, verify=False)


def parse_prometheus_data(file_path):
    content = {}
    with open(file_path) as f:
        for line in f.readlines():
            launch_name, num = line.strip('\n').split(' ')
            content[launch_name] = num
    return content


def generate_message_content(content):
    passed_num = content['launch_status_passed']
    failed_num = content['launch_status_failed']
    broken_num = content['launch_status_broken']
    skipped_num = content['launch_status_skipped']
    case_num = content['launch_retries_run']
    message_content = {
        "msg_type": "text",
        "content": {
            "text": f"自动化执行结果：\n日期：{Getdate().getCurrentday()} {Getdate().getWeekdayToday()}\n"
                    f"通过数量：{passed_num}\n"
                    f"失败数量：{failed_num}\n"
                    f"阻塞数量：{broken_num}\n"
                    f"跳过数量：{skipped_num}\n"
                    f"总数量：{case_num}\n"
                    f"通过率：{int(passed_num) / int(int(case_num) - int(skipped_num)) * 100}% \n"
        }
    }
    return message_content

def send_test_report():
    """ 发送测试报告到通知群 """
    content = parse_prometheus_data(allure_report_prometheusData)
    message_content = generate_message_content(content)
    webhook_test = LoadIni("notify_variable.ini").getSection('feishu').get('webhook_test')
    push_message(webhook_test, message_content)


if __name__ == '__main__':
    send_test_report()
